<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\SLessonInfo;
use Carbon\Carbon;

class SendLessonBeforNotic extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:SendLessonBeforNotic';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每10分钟运行一次，课前一小时发送给学生上课提醒';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //时间上重复5分钟，每次查询15分钟以内的课程
        $start = Carbon::now()->addMinutes(-5)->toDateTimeString();
        $end   = Carbon::now()->addMinutes(10)->toDateTimeString();
        logger('SendLessonBeforNoticStart:'.$start);
        logger('SendLessonBeforNoticEnd:'.$end);

        $lessons = SLessonInfo::where([
            ['begin_time', '>', $start],
            ['begin_time', '<=', $end],
            ['lesson_status', 0],           //未上课
            ['lesson_cancle', 0],           //未请假
            ['send_notice', 1 ]            //待发送
        ])->get();

        foreach($lessons as $lesson) {
            //发送短信
            dispatch( new \App\Jobs\SendPhoneMsg( $lesson->users->phone, 'lesson_befor_notice_stu', [ 'code' => 1235] ) );
            $lesson->send_notice = 2;
            $lesson->save();
        }

    }


}
